package com.xoa.service.party;


import com.xoa.dao.party.EnSafetyRegulationMapper;
import com.xoa.model.party.EnSafetyRegulation;
import com.xoa.model.users.Users;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gsb on 2018/1/10.
 */
@Service
public class EnSafetyRegulationService {

    @Resource
    private EnSafetyRegulationMapper enSafetyRegulationMapper;

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10 下午14:40:51
     * 类介绍  :   查找登陆人的信息
     * 构造参数:
     *
     */
    public ToJson<EnSafetyRegulation> queryUser(HttpServletRequest request){
        ToJson<EnSafetyRegulation> json = new ToJson<>();
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            EnSafetyRegulation enSafetyRegulation = new EnSafetyRegulation();
            enSafetyRegulation.setUsers(user);
            json.setObject(enSafetyRegulation);
            json.setFlag(0);
            json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10 下午14:46:51
     * 类介绍  :   新建
     * 构造参数:
     *
     */
    public ToJson<EnSafetyRegulation> insertSelective(EnSafetyRegulation enSafetyRegulation, HttpServletRequest request){
        ToJson<EnSafetyRegulation> json = new ToJson<>();
        try {
//            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
//            enSafetyRegulation.setPublishDepartment(String.valueOf(user.getDeptId()));
//            enSafetyRegulation.setPublisher(user.getUserId());
//            enSafetyRegulation.setPublishTime(DateFormat.getCurrentTime2());
            enSafetyRegulationMapper.insertSelective(enSafetyRegulation);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10 下午14:46:51
     * 类介绍  :   删除
     * 构造参数:
     *
     */
    public ToJson<EnSafetyRegulation> deleteByPrimaryKey(Integer rid){
        ToJson<EnSafetyRegulation> json = new ToJson<>();
        try {
            enSafetyRegulationMapper.deleteByPrimaryKey(rid);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10 下午14:46:51
     * 类介绍  :   修改
     * 构造参数:
     *
     */
    public ToJson<EnSafetyRegulation> updateByPrimaryKeySelective(EnSafetyRegulation enSafetyRegulation){
        ToJson<EnSafetyRegulation> json = new ToJson<>();
        try {
            enSafetyRegulationMapper.updateByPrimaryKeySelective(enSafetyRegulation);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10 下午14:46:51
     * 类介绍  :   详情
     * 构造参数:
     *
     */
    public ToJson<EnSafetyRegulation> selectById(Integer rid, HttpServletRequest request) {
        ToJson<EnSafetyRegulation> json = new ToJson<>();
        try {

            EnSafetyRegulation enSafetyRegulation = enSafetyRegulationMapper.selectById(rid);
            String sqlType = "xoa"
                    + (String) request.getSession().getAttribute("loginDateSouse");
            if(enSafetyRegulation!=null){
                if (enSafetyRegulation.getAttachmentName() != null && enSafetyRegulation.getAttachmentId() != null) {
                    enSafetyRegulation.setAttachments1(GetAttachmentListUtil.returnAttachment(enSafetyRegulation.getAttachmentName(), enSafetyRegulation.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_parameter));
                }
            }
            json.setObject(enSafetyRegulation);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-10 下午15:16:51
     * 类介绍  :   列表
     * 构造参数:
     *
     */
    public ToJson<EnSafetyRegulation> queryList(EnSafetyRegulation enSafetyRegulation, Integer page, Integer pageSize,
                                                boolean useFlag, HttpServletRequest request, HttpServletResponse response, String export){
        ToJson<EnSafetyRegulation> json = new ToJson<>();
        try {
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            if(useFlag){
                map.put("page", pageParams);
            }
            map.put("enSafetyRegulation",enSafetyRegulation);
            List<EnSafetyRegulation> enSafetyRegulationList = enSafetyRegulationMapper.selectList(map);
            json.setObj(enSafetyRegulationList);
            json.setTotleNum(enSafetyRegulationMapper.selectCount(enSafetyRegulation));
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
}
